/*
 * RSendAs_SMS.cpp
 *
 *  Created on: 2011-4-15
 *      Author: user
 */

#include "RSendAs_SMS.h"
#include <e32cmn.h>
#include <smut.h>
#include "debugHelper.h"
CRSendAsSender* CRSendAsSender::NewL( MRSendSMSObserver& aObserver, const TDesC& aMessage, const TDesC& aRecipients )
{
    CRSendAsSender* self = new ( ELeave ) CRSendAsSender( aObserver );
    CleanupStack::PushL( self );
    self->ConstructL( aMessage, aRecipients );
    CleanupStack::Pop( self );
    return self;
}

CRSendAsSender::CRSendAsSender( MRSendSMSObserver& aObserver ) :
    CActive( 0 ), iObserver( aObserver )
{
}

CRSendAsSender::~CRSendAsSender( )
{
    Cancel( );
    iSendAsMessage.Close( );
    iSendAs.Close( );
    delete iSession; // session must be deleted last
}

void CRSendAsSender::ConstructL( const TDesC& aMessage, const TDesC& aRecipients )
{
    CActiveScheduler::Add( this );
    
    User::LeaveIfError( iSendAs.Connect( ) );
    iSendAsMessage.CreateL( iSendAs, KUidMsgTypeSMS );
    
    iSendAsMessage.AddRecipientL( aRecipients, RSendAsMessage::ESendAsRecipientTo );
    iSendAsMessage.SetBodyTextL( aMessage );
    
    iSendAsMessage.SendMessage( iStatus );
    //DebugError( "MessageSentL" );
    // Session to message server is opened asynchronously.
    iSession = CMsvSession::OpenAsyncL( *this );
    
    SetActive( );
}

void CRSendAsSender::DoCancel( )
{
    DebugError( "CRSendAsSender::DoCancel" );
    iSendAsMessage.Cancel( );
}

void CRSendAsSender::RunL( )
{
    DebugError( "CRSendAsSender::RunL" );
    //iObserver.MessageSentL( iStatus.Int( ) );
}
void CRSendAsSender::HandleSessionEventL( TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3 )
{
    static char* eventStrings[ ] =
        {
                "EMsvEntriesCreated",
                "EMsvEntriesChanged",
                "EMsvEntriesDeleted",
                "EMsvEntriesMoved",
                "EMsvMtmGroupInstalled",
                "EMsvMtmGroupDeInstalled",
                "EMsvGeneralError",
                "EMsvCloseSession",
                "EMsvServerReady",
                "EMsvServerFailedToStart",
                "EMsvCorruptedIndexRebuilt",
                "EMsvServerTerminated",
                "EMsvMediaChanged",
                "EMsvMediaUnavailable",
                "EMsvMediaAvailable",
                "EMsvMediaIncorrect",
                "EMsvCorruptedIndexRebuilding"
        };
    const int kEventStringCount = sizeof( eventStrings ) / sizeof( eventStrings[ 0 ] );
    int eventIndex = ( int )aEvent;
    if( eventIndex >= 0 && eventIndex <= kEventStringCount - 1 )
    {
        DebugError( eventStrings[eventIndex] );
    }
    else
    {
        DebugError( eventIndex );
    }
    switch( aEvent )
    {
        // Session to server established
        case EMsvServerReady:
            break;
        case EMsvEntriesMoved:
            {
                if( aArg2 && aArg3 )
                {
                    TMsvId newId = *( static_cast< TMsvId* > ( aArg2 ) );
                    TMsvId oldId = *( static_cast< TMsvId* > ( aArg3 ) );
                    //DebugError( newId );
                    //DebugError( oldId );
                    if( newId == KMsvSentEntryId && oldId == KMsvGlobalOutBoxIndexEntryId )
                    {
                        //DebugError("");
                        CMsvEntrySelection* entries = static_cast< CMsvEntrySelection* > ( aArg1 );
                        if( entries == NULL )
                        {
                            break;
                        }
                        TInt count = entries->Count( );
                        for( int i = 0; i < count; i++ )
                        {
                            TMsvId entryID = entries->At( i );
                            CMsvEntry* entry = iSession->GetEntryL( ( *entries )[ i ] );
                            CleanupStack::PushL( entry );
                            entry->DeleteL( entryID );
                            CleanupStack::PopAndDestroy( entry );
                        }
                        //DebugError("");
                        iObserver.MessageSentL( 0 );
                    }
                }
            }
            break;
        case EMsvGeneralError:
            {
                iObserver.MessageSentL( -1 );
            }
            break;
        default:
            break;
    }
}
